package com.xm.service.service.impl;

import com.alibaba.excel.EasyExcel;
import com.xm.listener.UserListener;
import com.xm.mapper.UserMapper;
import com.xm.pojo.User;
import com.xm.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * 业务逻辑层（service）：处理业务，做一些判断等等...
 */
@Service//PatientService ps = new PatientServiceImpl(); -> ps对象已经放到spring容器中
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public List<User> findAll() {
        return userMapper.findAll();
    }

    @Override
    public void add(User user) {
        userMapper.add(user);
    }

    @Override
    public void logicDelete(User user) {
        userMapper.logicDelete(user);
    }

    @Override
    public void updateUser(User user) {
        userMapper.updateUser(user);
    }

    @Override
    public void batchDel(Integer[] ids) {
        userMapper.batchDel(ids);
    }

    @Override
    public List<User> findByIds(Integer[] ids) {
        return userMapper.findByIds(ids);
    }

    @Override
    public void upload(MultipartFile file) {
        InputStream inputStream = null;

        //获取输入流
        try {
            inputStream = file.getInputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }

        // 这里 需要指定读用哪个class去读，然后读取第一个sheet 文件流会自动关闭
        EasyExcel.read(inputStream,User.class, new UserListener(userMapper)).sheet().doRead();
    }

    @Override
    public List<User> findDeleted() {
        return userMapper.findDeleted();
    }

    @Override
    public void restoreById(User user) {
        userMapper.restoreById(user);
    }

    @Override
    public void batchRes(Integer[] ids) {
        userMapper.batchRes(ids);
    }

    @Override
    public void deleteById(String id) {
        userMapper.deleteById(id);
    }

    @Override
    public List<User> findById(String v) {
        return userMapper.findById(v);
    }

    @Override
    public List<User> findByUsername(String v) {
        return userMapper.findByUsername(v);
    }

    @Override
    public List<User> findByNick(String v) {
        return userMapper.findByNick(v);
    }

    @Override
    public List<User> findByPhone(String v) {
        return userMapper.findByPhone(v);
    }

    @Override
    public Integer getuserNum() {
        return userMapper.getuserNum();
    }

    @Override
    public String getImg() {
        return userMapper.getImg();
    }


}
